import requests
import parsel


class SongCrawler:

    def __init__(self, url="奢香夫人"):
        self.url = url
        self.headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36",
        }
        self.html = ""

    def get_data(self):
        try:
            response = requests.get(url=self.url, headers=self.headers)
            self.html = response.text
        except Exception as e:
            print(e)

    def parser_data(self):
        selector = parsel.Selector(text=self.html)
        song_url_list = selector.css(".col-3.col-content a::attr(href)").getall()
        return song_url_list

    def run(self):
        self.get_data()
        return self.parser_data()


def search_songs(song):
    search = f"https://www.gequbao.com/s/{song}"
    spider = SongCrawler(search)
    songs = spider.run()
    lst = [f"https://www.gequbao.com{x}" for x in songs]
    print(lst)
    return lst


if __name__ == '__main__':
    song = input('请输入歌曲名称：')
    search = f"https://www.gequbao.com/s/{song}"
    spider = SongCrawler(search)
    songs = spider.run()
    lst = [f"https://www.gequbao.com{x}" for x in songs]
    print(lst)

